package com.dearbinge.data.service.impl.shopManage;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.shopManage.ShopManageService;
import com.dearbinge.data.dao.OrderDetailTableMapper;
import com.dearbinge.data.dao.ShopManageMapper;
import com.dearbinge.data.dao.UserMapper;
import com.dearbinge.data.dao.thumbsCommission.ThumbsCommissionMapper;
import com.dearbinge.data.pojo.shopManage.ShopManage;
import com.dearbinge.data.pojo.thumbsCommission.ThumbsCommission;
import com.dearbinge.data.pojo.user.User;
import com.dearbinge.utils.Constants;
import com.dearbinge.utils.PageResult;
import com.dearbinge.utils.enums.TypeCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Transactional
@Service(value = "shopManageService")
public class ShopManageServiceImpl implements ShopManageService {

    @Autowired
    private ShopManageMapper shopManageMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ThumbsCommissionMapper thumbsCommissionMapper;

    @Autowired
    private OrderDetailTableMapper orderDetailTableMapper;


    @Override
    public Integer update(ShopManage shopManage) {
        Integer dataCode = shopManageMapper.update(shopManage);
        if (dataCode > 0)updateUser(shopManage);
        return dataCode;
    }

    @Override
    public Integer insert(ShopManage shopManage) {
        Integer dataCode = shopManageMapper.insert(shopManage);
        if (dataCode > 0)updateUser(shopManage);
        //开店成功 增加开店佣金
        /*if (dataCode>1) {
            insertCommission(shopManage);
            return shopManage.getId();
        }*/
        return dataCode;
    }

    /**
     * 修改用户是否开店的状态
     * @param shopManage 店铺实体类
     */
    public void updateUser(ShopManage shopManage){
            User user = new User();
            user.setId(Integer.parseInt(shopManage.getUserId()));
            user.setIfShop(true);
            userMapper.update(user);
    }

    /**
     * 邀请开店 增加佣金
     * @param shopManage 店铺
     */
    private void insertCommission(ShopManage shopManage) {
        //查询该上家
        List<User> users = userMapper.listUserByLevel(Integer.valueOf(shopManage.getUserId()));
        if (users !=null){
            ThumbsCommission thumbsCommission = new ThumbsCommission();
            //类型
            thumbsCommission.setOperateType( TypeCodeEnum.COMMISSION_SHOP.getCode());
            thumbsCommission.setCommissionStatus(TypeCodeEnum.COMMISSION_STATUS_IN.getCode());
            for (int i = 0; i < users.size(); i++) {
                BigDecimal totalCommission = null;
                User user = users.get(i);
                thumbsCommission.setPublishUserId(user.getId().toString());
                if (i == 0){
                    thumbsCommission.setRewardAmount(Constants.SHOP_COMMISSION_FIRST);
                     totalCommission = user.getTotalCommission().add(Constants.SHOP_COMMISSION_FIRST);
                }
                if (i == 1){
                    thumbsCommission.setRewardAmount(Constants.SHOP_COMMISSION_SECOND);
                     totalCommission = user.getTotalCommission().add(Constants.SHOP_COMMISSION_SECOND);
                }
                thumbsCommissionMapper.insert(thumbsCommission);
                //更新用户表佣金
                user.setTotalCommission(totalCommission);
                userMapper.update(user);
            }
        }
    }

    @Override
    public ShopManage getByshopCode(String shopCode, String shopId) {
        ShopManage shopManage = shopManageMapper.getByshopCode(shopCode,shopId);
        return shopManage;
    }

    @Override
    public Integer deleted(String id) {
        Integer dataCode = shopManageMapper.deleted(id);
        return dataCode;
    }

    @Override
    public ShopManage getByUserId(String userId) {
        ShopManage shopManage = shopManageMapper.getByUserId(userId);
        return shopManage;
    }

    @Override
    public PageResult<List<Map<String, Object>>> getComByUserId(String userId,Integer pageNo,Integer pageSize) {
        pageNo = (null == pageNo || pageNo < 1) ? 1 : pageNo;
        pageSize = (null == pageSize || pageSize < 0) ? 10 :pageSize;
        Integer count = shopManageMapper.count(userId);
        Page page = new Page(pageNo,pageSize);
        List<Map<String,Object>> mapperList = shopManageMapper.getComByUserId(userId,page);
        if (mapperList != null) {
            mapperList = getPrice(mapperList);
            for (int index = 0;index < mapperList.size();index++){
                String commodityId = mapperList.get(index).get("id").toString();
                Integer salesVolume = orderDetailTableMapper.salesVolume(commodityId);
                mapperList.get(index).put("salesVolume",salesVolume);
            }
        }
        Integer max = count % pageSize == 0 ? count / pageSize : count / pageSize +1;
        PageResult<List<Map<String,Object>>> pageResult = new PageResult<List<Map<String,Object>>>(mapperList);
        pageResult.setMaxPageSize(max);
        return pageResult;
    }

    @Override
    public PageResult<List<Map<String, Object>>> getNotCopyCommdityId(String shopId, String copyId, Integer pageNo, Integer pageSize) {
        pageNo = (null == pageNo || pageNo < 1) ? 1 : pageNo;
        pageSize = (null == pageSize || pageSize < 0) ? 10 :pageSize;
        Integer count = shopManageMapper.NotCopyCommdityIdCount(shopId,copyId);
        Page page = new Page(pageNo,pageSize);
        List<Map<String,Object>> mapperList = shopManageMapper.getNotCopyCommdityId(shopId,copyId,page);
        if (mapperList != null) {
            mapperList = getPrice(mapperList);
        }
        Integer max = count % pageSize == 0 ? count / pageSize : count / pageSize +1;
        PageResult<List<Map<String,Object>>> pageResult = new PageResult<List<Map<String,Object>>>(mapperList);
        pageResult.setMaxPageSize(max);
        return pageResult;
    }


    public List<Map<String,Object>> getPrice(List<Map<String,Object>> mapperList){
        try {
            for (Map<String,Object> mp : mapperList){
                Double couponDiscountPct = null;
                if (mp != null) {
                    Double cCwholesalePct = Double.valueOf(mp.get("CCwholesalePct").toString());
                    Double cCcouponPct = Double.valueOf(mp.get("CCcouponPct").toString());
                    Double retailPrice = Double.valueOf(mp.get("retailPrice").toString());
                    Double forwardpct = Double.valueOf(mp.get("CCforwardPct").toString());
                    couponDiscountPct =(1 - cCwholesalePct) * retailPrice;
                    mp.put("coupon",null);
                    mp.put("commission",null);
                    if (couponDiscountPct != null){
                        Double coupon = cCcouponPct * couponDiscountPct;
                        BigDecimal couponPct = new BigDecimal(coupon).setScale(2, BigDecimal.ROUND_UP);
                        mp.put("couponPrice",couponPct);
                        Double commission = forwardpct * couponDiscountPct;
                        BigDecimal commissionPct = new BigDecimal(commission).setScale(2, BigDecimal.ROUND_UP);
                        mp.put("commissionPrice",commissionPct);
                    }
                }
            }
            return mapperList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
